package com.mysdnu.customview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mysdnu.mysdnu_android.R;
import com.mysdnu.utils.DensityUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class TitleBarView extends RelativeLayout {
	private TextView textView;
	private ImageView imageView;
	private DisplayImageOptions options = null;
	private ImageLoader imageLoader = null;
	private ImageView menuImage;

	public TitleBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		// 初始化titlebar
		LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		textView = new TextView(context);
		textView.setText("跳蚤市场");
		textView.setTextColor(Color.WHITE);
		textParams.addRule(CENTER_IN_PARENT);
		textView.setTextSize(20);
		textView.setLayoutParams(textParams);
		addView(textView);

		LayoutParams imageParams = new LayoutParams(DensityUtil.dip2px(context,
				40), DensityUtil.dip2px(context, 40));
		imageView = new ImageView(context);
		imageParams.addRule(CENTER_VERTICAL);
		// imageView.setImageResource(R.drawable.user_default);
		imageView.setLayoutParams(imageParams);
		imageView.setBackgroundColor(Color.WHITE);
		imageView.setScaleType(ScaleType.CENTER_CROP);
		initUIL();
		addView(imageView);

		menuImage = new ImageView(context);
		LayoutParams menuParams = new LayoutParams(DensityUtil.dip2px(context,
				40), DensityUtil.dip2px(context, 40));
		menuParams.addRule(CENTER_VERTICAL);
		menuParams.addRule(ALIGN_PARENT_RIGHT);
		menuImage.setLayoutParams(menuParams);
		menuImage.setImageResource(R.drawable.ic_menu_up);
		menuImage.setBackgroundColor(Color.WHITE);
		menuImage.setScaleType(ScaleType.CENTER_CROP);
		addView(menuImage);
	}

	private void initUIL() {
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.user_default)
				.showImageOnFail(R.drawable.user_default).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(50)).build();
		imageLoader.displayImage("assets://user_.png", imageView);
		// imageLoader.displayImage(uri, imageView);
	}

	public void setUserImageClickListener(OnClickListener l) {
		imageView.setOnClickListener(l);
	}

	public void setUserImageVisibility(int visibility) {
		imageView.setVisibility(visibility);
	}

	public void setMenuClickListener(OnClickListener l) {
		menuImage.setOnClickListener(l);
	}

	public void setMenuVisibility(int visibility) {
		menuImage.setVisibility(visibility);
	}
}
